import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;

/**
 * A JPanel for viewing who is currently logged in.
 *
 * @author Jacel Evasco
 * @author Ace Dicca
 * @author Roselle Nympha Lim
 * @version 1.0
 */
public class ProfilePeeker extends JPanel {

    private JLabel iD = new JLabel();
    private JLabel subtitle = new JLabel("logged in as");
    private JButton logout = new JButton(new ImageIcon(
        "images\\icons\\logoutNormal.png"));
    private JPanel center = new JPanel();
    private JPanel buttons = new JPanel();

    Connection connection = null;
    ResultSet resultSet = null;
    PreparedStatement preparedStatement = null;

    public ProfilePeeker() {
        assemble();
        configure();
        design();
        setTriggers();
    }// end of ProfilePeeker()

    private void prepareConnection() {
        try {
            connection = DatabaseUtility.prepareConnection();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e);
        }
    } // end of prepareConnection method

    private void assemble() {

        // buttons
        buttons.setLayout(new BoxLayout(buttons, BoxLayout.X_AXIS));
        buttons.add(Box.createRigidArea(new Dimension(234, 0)));
        buttons.add(logout);
        // center
        center.setLayout(new BoxLayout(center, BoxLayout.Y_AXIS));
        center.add(Box.createRigidArea(new Dimension(0, 55)));
        center.add(subtitle);
        center.add(Box.createRigidArea(new Dimension(0, 1)));
        center.add(iD);
        center.add(Box.createRigidArea(new Dimension(0, 25)));
        center.add(buttons);
        // this
        this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        this.add(center);
    }// end of assemble()

    private void configure() {
        fetchID();
    }// end of configure()

    private void design() {
        // sizes
        subtitle.setPreferredSize(new Dimension(500, 50));
        subtitle.setMinimumSize(new Dimension(500, 50));
        subtitle.setMaximumSize(new Dimension(500, 50));
        iD.setPreferredSize(new Dimension(500, 50));
        iD.setMinimumSize(new Dimension(500, 50));
        iD.setMaximumSize(new Dimension(500, 50));
        logout.setPreferredSize(new Dimension(32, 32));
        logout.setMinimumSize(new Dimension(32, 32));
        logout.setMaximumSize(new Dimension(32, 32));
        buttons.setPreferredSize(new Dimension(500, 32));
        buttons.setMinimumSize(new Dimension(500, 32));
        buttons.setMaximumSize(new Dimension(500, 32));
        center.setPreferredSize(new Dimension(500, 284));
        center.setMinimumSize(new Dimension(500, 284));
        center.setMaximumSize(new Dimension(500, 284));
        this.setPreferredSize(new Dimension(500, 284));
        this.setMinimumSize(new Dimension(500, 284));
        this.setMaximumSize(new Dimension(500, 284));
        // alignments
        iD.setHorizontalAlignment(JLabel.CENTER);
        iD.setVerticalAlignment(JLabel.CENTER);
        subtitle.setHorizontalAlignment(JLabel.CENTER);
        subtitle.setVerticalAlignment(JLabel.CENTER);
        iD.setAlignmentX(Component.CENTER_ALIGNMENT);
        subtitle.setAlignmentX(Component.CENTER_ALIGNMENT);
        // font styles
        iD.setFont(new Font("Segoe UI Semibold", Font.BOLD, 50));
        subtitle.setFont(new Font("Segoe UI", Font.PLAIN, 20));
        // backgrounds
        center.setOpaque(false);
        buttons.setOpaque(false);
        // button styles
        logout.setOpaque(false);
        logout.setContentAreaFilled(false);
        logout.setBorderPainted(false);

    }// end of design()

    public void fetchID() {
        prepareConnection();
        String query = "SELECT * FROM requirementstrackerschema.student"
            + " WHERE isLoggedIn = 1";
        try {
            preparedStatement = connection.prepareStatement(query);
            resultSet = preparedStatement.executeQuery();

            if (resultSet.next()) {
                String profileID = resultSet.getString("studentID");
                iD.setText(profileID);
            } else {
                iD.setText("");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } // end of try-catch
    }// end of fetchID()

    private void setTriggers() {
        MouseListener viewerTrigger = new MouseListener() {
            public void mouseClicked(MouseEvent me) {
                if (me.getSource() == logout) {
                    prepareConnection();

                    String updateStatus = "UPDATE requirementstrackerschema.student"
                        + " SET isLoggedIn = '0' WHERE isLoggedIn = '1'";

                    try {
                        preparedStatement = connection
                            .prepareStatement(updateStatus);
                        preparedStatement.executeUpdate();

                        Explorer.getInstance().fetchID();
                        Explorer.getInstance().getProfileMenu()
                            .getProfilePeeker().fetchID();
                        Explorer.getInstance().getProfileMenu()
                            .getProfileEditor().cleanUp();
                        Explorer.getInstance().getProfileMenu()
                            .getProfileEditor().fetchID();
                        Explorer.getInstance().getProfileMenu()
                            .getProfileAuthenticator().cleanUp();
                        Explorer.getInstance().getProfileMenu()
                            .getProfileMaker().cleanUp();
                        Explorer.getInstance()
                            .getRequirementsViewer().getData()
                            .refresh();
                        Explorer.getInstance().getGoalsViewer()
                            .getData().refresh();
                        Explorer.getInstance().getProfileMenu()
                            .getEdit().setEnabled(false);
                        Explorer.getInstance().getProfileMenu()
                            .getEdit().setVisible(false);
                        Explorer.getInstance().getProfileMenu()
                            .loginOrLogout();
                        Explorer.getInstance().getGoalsViewer()
                            .isDataEmpty();
                        Explorer.getInstance()
                            .getRequirementsViewer().isDataEmpty();
                        // show profileAuthenticator
                        ((CardLayout) (ProfileMenu.getCards()
                            .getLayout())).show(
                            ProfileMenu.getCards(),
                            "profileAuthenticator");
                    } catch (Exception e) {
                        e.printStackTrace();
                    } // end of try-catch
                }
            } // end of mouseClicked()

            public void mouseEntered(MouseEvent me) {
                if (me.getSource() == logout) {
                    logout.setIcon(new ImageIcon(
                        "images\\icons\\logoutEntered.png"));
                }
            } // end of mouseEntered()

            public void mouseExited(MouseEvent me) {
                if (me.getSource() == logout) {
                    logout.setIcon(new ImageIcon(
                        "images\\icons\\logoutNormal.png"));
                }
            } // end of mouseExited()

            public void mousePressed(MouseEvent me) {
                if (me.getSource() == logout) {
                    logout.setIcon(new ImageIcon(
                        "images\\icons\\logoutPressed.png"));
                }
            } // end of mousePressed()

            public void mouseReleased(MouseEvent me) {
                if (me.getSource() == logout) {
                    logout.setIcon(new ImageIcon(
                        "images\\icons\\logoutNormal.png"));
                }
            }
        };
        logout.addMouseListener(viewerTrigger);

    }// end of setTriggers()

}// end of ProfilePeeker